local tian = fk.CreateSkill {
    name = "ym1__tian",
}
tian:addEffect(fk.CardUseFinished,{
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(tian.name) then
            local room = player.room
            local choices = table.map(player:getTableMark("@ym1__tian"), function (choice)
                return "#"..string.sub(choice, 2)
            end)
            if #choices == 4 then
                table.removeOne(choices,"#ym1__tian4")
            end
            local targets = table.filter(room.alive_players, function (p)
                return #p:getCardIds("ej") > 0
            end)
            if #targets == 0 then table.removeOne(choices,"#ym1__tian3") end
            table.removeOne(choices,"#ym1__tian1")
            return target == player and data.card.trueName == "slash" and not(data.extra_data and data.extra_data.ym1__tian) and #choices > 0
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local choices, all_choices = table.map(player:getTableMark("@ym1__tian"), function (choice)
            return "#"..string.sub(choice, 2)
        end),table.map(player:getTableMark("@ym1__tian"), function (choice)
            return "#"..string.sub(choice, 2)
        end)
        table.removeOne(choices,"#ym1__tian1")
        if table.contains(choices,"#ym1__tian3") then
            local targets = table.filter(room.alive_players, function (p)
                return #p:getCardIds("ej") > 0
            end)
            if #targets == 0 then table.removeOne(choices,"#ym1__tian3") end
        end
        if #choices > 0 then
            table.insert(choices,"Cancel")
            table.insert(all_choices,"Cancel")
            local result = room:askToChoice(player,{
                choices = choices,
                skill_name = tian.name,
                prompt = "#ym1__fa-prompt",
                all_choices = all_choices,
            })
            if result ~= "Cancel" then
                self.cost_data = result
                return true
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local marks = player:getTableMark("@ym1__tian")
        table.removeOne(marks,"@"..string.sub(self.cost_data, 2))
        room:setPlayerMark(player, "@ym1__tian",marks)
        if self.cost_data == "#ym1__tian1" then
            local use = room:askToUseCard(player, {
                pattern = "slash",
                skill_name = tian.name,
                prompt = "#ym1__tian-slash",
                cancelable = true,
            })
            if use then
                use.extraUse = true
                use.extra_data = use.extra_data or {}
                use.extra_data.ym1__tian = true
                room:useCard(use)
            end
        elseif self.cost_data == "#ym1__tian2" then
            player:drawCards(1, tian.name)
        elseif self.cost_data == "#ym1__tian3" then
            local targets = table.filter(room.alive_players, function (p)
                return #p:getCardIds("ej") > 0
            end)
            if #targets > 0 then
                local to = room:askToChoosePlayers(player,{
                    skill_name = tian.name,
                    targets = targets,
                    min_num = 1,
                    max_num = 1,
                    prompt = "#ym1__tian-discard",
                    cancelable = false,
                })
                to = room:getPlayerById(to[1].id)
                local card = #to:getCardIds("ej") == 1 and to:getCardIds("ej") or room:askToChooseCard(player,{
                    target = to,
                    flag = "ej",
                    skill_name = tian.name,
                    prompt = "#ym1__tian-discard"
                })
                room:throwCard(card, tian.name, to, player)
            end
        else
            room:setPlayerMark(player, "@ym1__tian", {"@ym1__tian1", "@ym1__tian2", "@ym1__tian3", "@ym1__tian4"})
        end
    end,
})
tian:addEffect(fk.AfterCardsMove,{
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(self) then
            local room = player.room
            local choices = table.map(player:getTableMark("@ym1__tian"), function (choice)
                return "#"..string.sub(choice, 2)
            end)
            if #choices == 4 then
                table.removeOne(choices,"#ym1__tian4")
            end
            local targets = table.filter(room.alive_players, function (p)
                return #p:getCardIds("ej") > 0
            end)
            if #targets == 0 then table.removeOne(choices,"#ym1__tian3") end
            for _, move in ipairs(data) do
                if move.moveReason == fk.ReasonDraw then
                    if move.to == player and move.skillName ~= tian.name and #move.moveInfo == 1 then
                        table.removeOne(choices,"#ym1__tian2")
                        if #choices > 0 then return true end
                    end
                elseif move.moveReason == fk.ReasonDiscard then
                    if #move.moveInfo > 0 and move.skillName ~= tian.name and ((move.proposer and  move.proposer == player) or (move.from and move.from == player)) then
                        for _, info in ipairs(move.moveInfo) do
                            if table.contains({Card.PlayerEquip,Card.PlayerJudge},info.fromArea) then
                                if table.contains(choices,"#ym1__tian3") then table.removeOne(choices,"#ym1__tian3") end
                                if #choices > 0 then return true end
                            end
                        end
                    end
                end
            end
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local choices, all_choices = table.map(player:getTableMark("@ym1__tian"), function (choice)
            return "#"..string.sub(choice, 2)
        end),table.map(player:getTableMark("@ym1__tian"), function (choice)
            return "#"..string.sub(choice, 2)
        end)
        for _, move in ipairs(data) do
            if move.moveReason == fk.ReasonDraw then
                table.removeOne(choices,"#ym1__tian2")
                break
            elseif move.moveReason == fk.ReasonDiscard then
                table.removeOne(choices,"#ym1__tian3")
                break
            end
        end
        if table.contains(choices,"#ym1__tian3") then
            local targets = table.filter(room.alive_players, function (p)
                return #p:getCardIds("ej") > 0
            end)
            if #targets == 0 then table.removeOne(choices,"#ym1__tian3") end
        end
        if #choices > 0 then
            table.insert(choices,"Cancel")
            table.insert(all_choices,"Cancel")
            local result = room:askToChoice(player,{
                choices = choices,
                skill_name = tian.name,
                prompt = "#ym1__fa-prompt",
                all_choices = all_choices,
            })
            if result ~= "Cancel" then
                self.cost_data = result
                return true
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local marks = player:getTableMark("@ym1__tian")
        table.removeOne(marks,"@"..string.sub(self.cost_data, 2))
        room:setPlayerMark(player, "@ym1__tian",marks)
        if self.cost_data == "#ym1__tian1" then
            local use = room:askToUseCard(player, {
                pattern = "slash",
                skill_name = tian.name,
                prompt = "#ym1__fa-slash",
                cancelable = true,
            })
            if use then
                use.extraUse = true
                use.extra_data = use.extra_data or {}
                use.extra_data.ym1__tian = true
                room:useCard(use)
            end
        elseif self.cost_data == "#ym1__tian2" then
            player:drawCards(1, tian.name)
        elseif self.cost_data == "#ym1__tian3" then
            local targets = table.filter(room.alive_players, function (p)
                return #p:getCardIds("ej") > 0
            end)
            if #targets > 0 then
                local to = room:askToChoosePlayers(player,{
                    skill_name = tian.name,
                    targets = targets,
                    min_num = 1,
                    max_num = 1,
                    prompt = "#ym1__fa-discard",
                    cancelable = false,
                })
                to = room:getPlayerById(to[1].id)
                local card = #to:getCardIds("ej") == 1 and to:getCardIds("ej") or room:askToChooseCard(player,{
                    target = to,
                    flag = "ej",
                    skill_name = tian.name,
                    prompt = "#ym1__fa-discard"
                })
                room:throwCard(card, tian.name, to, player)
            end
        else
            room:setPlayerMark(player, "@ym1__tian", {"@ym1__tian1", "@ym1__tian2", "@ym1__tian3", "@ym1__tian4"})
        end
    end,
})
tian:addEffect(fk.RoundEnd,{
    can_refresh = function (self, event, target, player, data)
        return player:getMark("ym1__zhu") ~= 0
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local skills = player:getMark("ym1__zhu")
        room:setPlayerMark(player,"ym1__zhu",0)
        room:handleAddLoseSkills(player,table.concat(skills,"|").."|-ym1__tian|-ym1__fa",nil,true)
    end,
})
tian:addEffect("targetmod",{
    residue_func = function(self, player, skill, scope)
        if player:hasSkill(tian.name) and skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
            return 1
        end
    end,
})
tian:addAcquireEffect(function (self, player, is_start)
    player.room:setPlayerMark(player, "@ym1__tian", {"@ym1__tian1", "@ym1__tian2", "@ym1__tian3", "@ym1__tian4"})
end)
tian:addLoseEffect(function (self, player, is_death)
    player.room:setPlayerMark(player, "@ym1__tian", 0)
end)
Fk:loadTranslationTable{
    ["ym1__tian"] = "罚",
    [":ym1__tian"] = "你使用【杀】的次数上限+1。你不因此技能执行以下一项结束后，可执行未选择过的另一项：1.使用一张【杀】；2.摸一张牌；3.弃置场上一张牌；4.重置此技能。",
    ["@ym1__tian"] = "罚",
    ["@ym1__tian1"] = "Ⅰ",
    ["#ym1__tian1"] = "使用一张【杀】",
    ["@ym1__tian2"] = "Ⅱ",
    ["#ym1__tian2"] = "摸一张牌",
    ["@ym1__tian3"] = "Ⅲ",
    ["#ym1__tian3"] = "弃置场上一张牌",
    ["@ym1__tian4"] = "Ⅳ",
    ["#ym1__tian4"] = "重置此技能",
}
return tian